package com.tansun.easycare.rule.datasource.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tansun.easycare.rule.common.JsonMessage;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.datasource.service.RuleDatasourceService;
import com.tansun.easycare.rule.deployonline.entity.RuleEnterpoint;
import com.tansun.easycare.rule.deployonline.service.RuleEnterpointService;
import com.tansun.easycare.rule.front.entity.EnterpointVo;
import com.tansun.easycare.rule.front.entity.RuleDatasource;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.ruletest.model.TestDataModeVO;
import com.tansun.easycare.rule.ruletest.service.RuleTestService;

@Controller
@RequestMapping(value = "/ruleProj/front")
public class FrontConfigController extends RuleBaseController{

	@Autowired
	private RuleDatasourceService ruleDatasourceService;
	@Autowired
	private RuleEnterpointService ruleEnterpointService;
	@Autowired
    private RuleManageService ruleManageService;
	@Autowired
    private RuleTestService ruleTestService;

	
	@RequestMapping(value = {"list", ""})
	public String list(RuleEnterpoint entity, HttpServletRequest request, HttpServletResponse response, Model model) {
		PageInfo<RuleEnterpoint> page = ruleEnterpointService.findPage(new Page<RuleEnterpoint>(), entity); 
		model.addAttribute("page", page);
		return "rule/front/enterpoint_list";
	}
	
	@RequestMapping("param")
	public String configParam(RuleEnterpoint entity, ModelMap model) {
		RuleEnterpoint enterpoint = this.ruleEnterpointService.get(entity);
		String ruleflowId = enterpoint.getRuleflowId().split("_")[0];
		String version = enterpoint.getRuleflowId().split("_")[1];
		RuleManage ruleflow = ruleManageService.findByCode(ruleflowId, enterpoint.getProjectCode());
		ruleflow.setVersion(version);
		TestDataModeVO dataModeVO = this.ruleTestService.findDataModeVO(ruleflow.getProjectCode(), ruleflowId, ruleflow.getModelType(), version);
		List<RuleDatasource> datasources = ruleDatasourceService.findList(new RuleDatasource());
		model.put("enterpoint", enterpoint);
		model.put("ruleflow", ruleflow);
		model.put("datasources", datasources);
		model.put("dataModeVO", dataModeVO);
		model.put("paramsMap", ruleDatasourceService.findParamMap(enterpoint));
		return "rule/front/enterpoint_param";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public JsonMessage savaOrUpdate(@RequestBody EnterpointVo vo){
		JsonMessage ret = new JsonMessage();
        try {
        	ruleDatasourceService.saveEnterParam(vo);
        } catch (Exception e) {
        	log.error("保存入口参数配置报错", e);
            ret.setResult(false);
            ret.setMessage(e.getMessage());
        }
        return ret;
	}
	
	
}
